package goroutine

import (
	"fmt"
	"sync"
)

var globalNumber int
var mutexGroup sync.WaitGroup
var lock sync.Mutex

func setGlobalNumber() {
	for i := 0; i < 10; i++ {
		lock.Lock()
		globalNumber += i
		lock.Unlock()
	}
	defer func() {
		lock.Lock()
		mutexGroup.Done()
		lock.Unlock()
	}()
}

func DemoMutex() {
	mutexGroup.Add(2)

	go setGlobalNumber()
	go setGlobalNumber()
	mutexGroup.Wait()
	fmt.Println(globalNumber)
}
